<script lang="ts" setup>
import { reactive, ref, watch } from "vue"
import { createTableDataApi, deleteTableDataApi, updateTableDataApi, getTableDataApi } from "@/api/table"
import { type CreateOrUpdateTableRequestData, type GetTableData } from "@/api/table/types/table"
import { type FormInstance, type FormRules, ElMessage, ElMessageBox } from "element-plus"
import { Search, Refresh, CirclePlus, Delete, Edit, RefreshRight } from "@element-plus/icons-vue"
import { usePagination } from "@/hooks/usePagination"
import { useRoute, useRouter } from "vue-router"

const s3 = [
'订单编号',
'产品工号',
'产品名称',
'物料名称',
'机号',
'配送人',
'部门',
'电话',
]
const tableData = ref([
  {
    email0: 'DD00000001',
    email1: '100200176',
    email2: '印度',
    email3: '前大梁',
    email4: '已完成',
    email5: '5#',
    email6: 'GYDE-2-3',
    email8: '1/2',
    // email8: 'B',
    email9: '23',
    email10: '涂装1#',
    email11: '张三',
    email12: '15899992222',
    email13: '',
    type: 'success',
    email14: '涂装',
    email15: '1车间',
    email16: '物料',
    email17: '2023-10-21 09:23:34',
    email19:'成毅'
  },
  {
    email0: 'DD00000001',
    email1: '100200176',
    email2: '印度',
    email3: '前大梁',
    email4: '已完成',
    email5: '5#',
    email6: 'GYDE-2-3',
    email8: '1/2',
    // email8: 'B',
    email9: '23',
    email10: '涂装1#',
    email11: '张三',
    email12: '15899992222',
    email13: '',
    type: 'success',
    email14: '涂装',
    email15: '1车间',
    email16: '物料',
    email17: '2023-10-21 09:23:34',
    email19:'成毅'
  },
  {
    email0: 'DD00000002',
    email1: '100200176',
    email2: '印度',
    email3: '前大梁',
    email4: '已完成',
    email5: '5#',
    email6: 'GYDE-2-3',
    email8: '1/2',
    // email8: 'B',
    email9: '23',
    email10: '涂装1#',
    email11: '张三',
    email12: '15899992222',
    email13: '',
    type: 'success',
    email14: '涂装',
    email15: '1车间',
    email16: '物料',
    email17: '2023-10-21 09:23:34',
    email19:'成毅'
  },
  {
    email0: 'DD00000002',
    email1: '100200176',
    email2: '印度',
    email3: '前大梁',
    email4: '已完成',
    email5: '5#',
    email6: 'GYDE-2-3',
    email8: '1/2',
    // email8: 'B',
    email9: '23',
    email10: '涂装1#',
    email11: '张三',
    email12: '15899992222',
    email13: '',
    type: 'success',
    email14: '涂装',
    email15: '1车间',
    email16: '物料',
    email17: '2023-10-21 09:23:34',
    email19:'成毅'
  }
])
defineOptions({
  // 命名当前组件
  name: "ElementPlus"
})

const router = useRouter()
function goto(path: string) {
  router.push({ path })
}

const activeName = ref("first0")
const s1 = [
'领料单',
'出库单',
'配送单',
'转运单',
'退库单',
'入库单',
'计划单',
'已完成',
]
const s2 = [
  {
    label: '申请部门',
    value: '生产部',
  },
  {
    label: '车间',
    value: '1车间',
  },
  {
    label: '班组',
    value: '2班组',
  },
  {
    label: '申请人',
    value: '张三',
  },
  {
    label: '电话',
    value: '13688889999',
  },

]

const loading = ref<boolean>(false)
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()

//#region 增
const DEFAULT_FORM_DATA: CreateOrUpdateTableRequestData = {
  id: undefined,
  username: "",
  password: ""
}
const dialogVisible = ref<boolean>(false)
const formRef = ref<FormInstance | null>(null)
const formData = ref<CreateOrUpdateTableRequestData>(JSON.parse(JSON.stringify(DEFAULT_FORM_DATA)))
const formRules: FormRules<CreateOrUpdateTableRequestData> = {
  username: [{ required: true, trigger: "blur", message: "请输入用户名" }],
  password: [{ required: true, trigger: "blur", message: "请输入密码" }]
}
const handleCreateOrUpdate = () => {
  formRef.value?.validate((valid: boolean, fields) => {
    if (!valid) return console.error("表单校验不通过", fields)
    loading.value = true
    const api = formData.value.id === undefined ? createTableDataApi : updateTableDataApi
    api(formData.value)
      .then(() => {
        ElMessage.success("操作成功")
        dialogVisible.value = false
        getTableData()
      })
      .finally(() => {
        loading.value = false
      })
  })
}
const resetForm = () => {
  formRef.value?.clearValidate()
  formData.value = JSON.parse(JSON.stringify(DEFAULT_FORM_DATA))
}
//#endregion

//#region 删
const handleDelete = (row: GetTableData) => {
  ElMessageBox.confirm(`确认删除？`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(() => {

  })
}
//#endregion

//#region 改
const handleUpdate = (row: GetTableData) => {
  dialogVisible.value = true
  formData.value = JSON.parse(JSON.stringify(row))
}
//#endregion

//#region 查
const searchFormRef = ref<FormInstance | null>(null)
const searchData = reactive({
  username: "",
  phone: ""
})
const getTableData = () => {
  loading.value = true
  getTableDataApi({
    currentPage: paginationData.currentPage,
    size: paginationData.pageSize,
    username: searchData.username || undefined,
    phone: searchData.phone || undefined
  })
    .then(({ data }) => {
      paginationData.total = data.total
      // tableData.value = data.list
    })
    .catch(() => {
      tableData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}
const handleSearch = () => {
  paginationData.currentPage === 1 ? getTableData() : (paginationData.currentPage = 1)
}
const resetSearch = () => {
  searchFormRef.value?.resetFields()
  handleSearch()
}
//#endregion
const gotoDetail = ()=>{
  props.isUrgent ? goto('/labels/urgentpeisong') : goto('/labels/peisong')
}
const props = defineProps({
  isUrgent: { type: Boolean, default: false }
})
/** 监听分页参数的变化 */
// watch([() => paginationData.currentPage, () => paginationData.pageSize], getTableData, { immediate: true })
</script>

<template>
  <div>
    <div shadow="never" class="search-wrapper">
      <el-form ref="searchFormRef" :inline="true" class="mt-2.5" label-width="auto" :model="searchData">
        <el-form-item prop="username" v-for="(item, index) in s3" :key="index" :label="item">
          <el-input v-model="searchData.username" placeholder="请输入" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
      <!-- <el-divider /> -->
    </div>
    <div v-loading="loading" shadow="never">
      <div class="toolbar-wrapper">
      <div>
        <el-button type="primary">导出全部数据</el-button>
        <el-button type="primary">导出选中数据</el-button>
      </div>
        <div>
          <el-tooltip content="刷新当前页">
            <el-button type="primary" :icon="RefreshRight" circle @click="getTableData" />
          </el-tooltip>
        </div>
      </div>
      <div class="table-wrapper">
        <el-table :data="tableData">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column prop="email0" label="订单编号" align="center" />
          <el-table-column prop="email14" label="部门" align="center" />
          <el-table-column prop="email15" label="车间" align="center" />
          <el-table-column prop="email11" label="申请人" align="center" />
          <el-table-column prop="email2" label="产品名称" align="center" />
          <el-table-column prop="email1" label="产品工号" align="center" width="100" />
          <el-table-column prop="email5" label="机号" align="center" />
          <el-table-column prop="email3" label="物料名称" align="center" />
          <el-table-column prop="email16" label="属性" align="center" />
          <el-table-column prop="email9" label="数量(铲)" align="center" />
          <el-table-column prop="email9" label="数量(件)" align="center" />
          <el-table-column prop="email10" label="发货地" align="center" />
          <el-table-column prop="email10" label="收货地" align="center" />
          <el-table-column prop="email11" label="收货人" align="center" />
          <el-table-column prop="email12" label="收货人电话" align="center" width="120" />
          <el-table-column prop="email19" label="组长" align="center" width="120" />
          <el-table-column prop="email11" label="组员(配送人)" align="center" width="120" />
          <el-table-column prop="email17" label="提交日期" align="center" />
          <el-table-column prop="email13" label="备注" align="center" />
          <el-table-column prop="" label="状态" align="center" width="130">
            <template #default="scope">
              <el-tag :type="scope.row.type" effect="plain">{{ scope.row.email4 }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="100" align="center">
            <template #default="scope" >
              <span style="color: #409eff; cursor: pointer;" @click="gotoDetail()">详情</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pager-wrapper">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :total="paginationData.total"
          :page-size="paginationData.pageSize"
          :currentPage="paginationData.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <!-- 新增/修改 -->
    <el-dialog
      v-model="dialogVisible"
      :title="formData.id === undefined ? '新增用户' : '修改用户'"
      @closed="resetForm"
      width="30%"
    >
      <el-form ref="formRef" :model="formData" :rules="formRules" label-width="100px" label-position="left">
        <el-form-item prop="username" label="用户名">
          <el-input v-model="formData.username" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="password" label="密码" v-if="formData.id === undefined">
          <el-input v-model="formData.password" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleCreateOrUpdate" :loading="loading">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style>
.el-divider--horizontal {
  margin: 0 0 18px 0;
}
</style>
<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}
</style>
